"""
@author: peng
@email: sb.peng@hzgosun.com
@file: gridMangeTest.py
@time: 2019/5/28 9:36

后台网格管理的增删改查测试用例
"""

import unittest
from time import sleep

from selenium.webdriver.common.keys import Keys

from common.functions import save_screenshot
from common.decorators import mkdir_classname,log4gs
from common.settings import TAG ,DATA_DIR
from common.vue.select import DropDownSelect,OperateSelect
from pages.gridManagePage import gridPage

@log4gs
@mkdir_classname(TAG)
class GridTest(unittest.TestCase):
    """网格管理功能"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 +"开始网格管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = gridPage('/DataManage/GridDataManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 +"结束网格管理功能测试".format(__class__.__name__)+ '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例： {} {}'.format(self.id(),self.shortDescription()))

    #@unittest.skip
    def test_page_1(self):
        """翻页查询"""
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)
            save_screenshot('翻页功能测试失败')

    #@unittest.skip
    def test_search_2(self):
        """网格名称搜索"""
        try:
            DropDownSelect(self.page.searchTools.keyword_dropdown_list,"网格名称")
            self.page.searchTools.keyword_input.send_keys("太原东桥社区网格")
            sleep(2)
            self.page.searchTools.search_btn.click()    #点击检索
            self.page.searchTools.keyword_input.clear()  # 清除网格名称输入框数据
            sleep(1)
            result = self.page.table.get_row("太原东桥社区网格")
            self.assertTrue(result,"通过网格编码查询到了：太原东桥社区网格")
            sleep(2)
        except Exception as e:
            print("网格名称搜索用例执行失败：%r" % e)
            save_screenshot(self,"网格名查询失败")

    #@unittest.skip
    def test_search_3(self):
        """网格编码查询"""
        try:
            DropDownSelect(self.page.searchTools.keyword_dropdown_list,"网格编码")
            self.page.searchTools.keyword_input.send_keys("45010201010101")
            sleep(2)
            self.page.searchTools.search_btn.click()    #点击检索
            self.page.searchTools.keyword_input.clear()
            sleep(1)
            result = self.page.table.get_row("45010201010101")
            self.assertTrue(result,"通过网格编码查询到了：45010201010101")
            sleep(2)
        except Exception as e:
            print("网格编码查询用例执行失败：%r" % e)
            save_screenshot(self, "网格编码查询失败")

    #@unittest.skip
    def test_search_4(self):
        """区域查询"""
        try:
            self.page.searchTools.grid_region.click()
            sleep(1)
            self.page.searchTools.grid_region_selector.select("山西省-太原市-小店区-小店街道-东桥社区")  #因为设置层级的问题，这里可能会找不到最里面的层级，这不是错误
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            result = self.page.table.get_row("1201010202")
            self.assertTrue(result, "通过区域:山西省-太原市-小店区-小店街道-东桥社区查询到了1201010202")
            sleep(1)
        except Exception as e:
            print("区域查询用例执行失败：%r" % e)
            save_screenshot(self,"区域查询失败")

    #@unittest.skip
    def test_search_5(self):
        """添加网格"""
        try:
            sleep(1)
            self.page.table.add_btn.click()
            sleep(1)
            self.page.addGrid.grid_name.send_keys("自动化测试创建的网格2")
            self.page.addGrid.grid_code.send_keys("360000000002")
            self.page.addGrid.leader_code.send_keys("36")
            self.page.addGrid.grid_region.click()
            self.page.addGrid.grid_region_selector.select("山西省-太原市-小店区-小店街道-大村社区")    #因为设置层级的问题，这里可能会找不到最里面的层级，这不是错误
            sleep(2)
            self.page.addGrid.grid_info.click()
            sleep(1)
            self.page.addGrid.gridMemberName.send_keys("自动化测试02")   #添加网格长
            self.page.addGrid.gridMemberPhone.send_keys("13011110002")     #网格长电话
            sleep(1)
            self.page.addGrid.gridNumber_info.click()
            sleep(1)
            self.page.addGrid.gridMemberName.send_keys("自动化测试03")    #添加网格员
            self.page.addGrid.gridMemberPhone.send_keys("13011110003")     #网格员电话

            sleep(1)
            self.page.addGrid.confirm.click()
            sleep(1)
            # 检验新增的网格
            DropDownSelect(self.page.searchTools.keyword_dropdown_list, "网格名称")
            self.page.searchTools.keyword_input.send_keys("自动化测试创建的网格2")
            sleep(2)
            self.page.searchTools.search_btn.click()  # 点击检索
            sleep(1)
            result = self.page.table.get_row("自动化测试创建的网格2")
            self.assertTrue(result, "通过网格名称查询到了：自动化测试创建的网格2")
            sleep(2)
        except Exception as e:
            print("添加网格用例执行失败：%r" % e)
            save_screenshot(self, "添加网格失败截图")

    @unittest.skip
    def test_modify_person_6(self):     #无法删除网格，导致网格信息修改后老是会重复，所以修改的意义不大
        """修改网格"""
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "网格名称")  # 关键字选择网格名称
        self.page.searchTools.keyword_input.send_keys("自动化测试创建的网格")  # 关键字输入
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        result = self.page.table.get_row("自动化测试创建的网格")
        OperateSelect(result, "编辑")
        sleep(1)  # 等待弹窗完全出来
        self.page.modifyGrid.grid_name.clear()
        self.page.modifyGrid.grid_name.send_keys("自动化修改网格信息")
        self.page.modifyGrid.grid_code.clear()
        self.page.modifyGrid.grid_code.send_keys("3601111111")
        self.page.modifyGrid.leader_code.clear()
        sleep(1)
        self.page.modifyGrid.leader_code.send_keys("36")
        """区域暂时清不掉,以下是代码，待完善 """
        # sleep(4)
        # DropDownSelect(self.page.searchTools.region_dropdown_list,"区域")
        # self.page.searchTools.region_region_input.click()
        # self.page.addGrid.grid_region.click()
        # sleep(4)
        # DropDownSelect(self.page.modifyGrid.region_dropdown_list, "区域")
        # self.page.modifyGrid.grid_region_input.click()
        # self.page.modifyGrid.grid_region_selector.select("山西省-太原市-小店区-小店街道-大村社区")
        # sleep(1)
        # self.page.modifyGrid.grid_region_input.clear()
        # sleep(1)
        # self.page.addGrid.region_region_input.clear()
        # sleep(3)
        # self.page.addGrid.grid_region.click()
        # sleep(1)
        # self.page.modifyGrid.grid_region_selector.select("江西省-南昌市-高新区-高校园区-江西现代学院")

        self.page.modifyGrid.grid_info.click()
        sleep(1)
        self.page.modifyGrid.gridMemberName.clear()
        self.page.modifyGrid.gridMemberPhone.clear()
        sleep(1)
        self.page.modifyGrid.gridMemberName.send_keys("自动化修改测试02")  # 添加网格长名字
        self.page.modifyGrid.gridMemberPhone.send_keys("13099990002")
        sleep(1)
        self.page.modifyGrid.gridNumber_info.click()
        sleep(1)
        self.page.modifyGrid.gridMemberName.clear()
        self.page.modifyGrid.gridMemberPhone.clear()
        sleep(1)
        self.page.modifyGrid.gridMemberName.send_keys("自动化修改测试03")  # 添加网格员名字
        self.page.modifyGrid.gridMemberPhone.send_keys("13099990003")

        sleep(1)
        # self.page.modifyGrid.confirm.click()  # 确定修改按钮
        # sleep(1)  # 等待弹窗消失完毕
        # # self.page.searchTools.reset_btn.click()
        # DropDownSelect(self.page.searchTools.keyword_dropdown_list, "网格名称")  # 关键字选择网格名称
        # self.page.searchTools.keyword_input.send_keys("自动化修改网格信息")  # 关键字输入
        # self.page.searchTools.search_btn.click()  # 点击检索按钮
        # sleep(1)
        # result = self.page.table.get_row("自动化修改网格信息")
        # self.assertTrue(result, "通过网格名称:自动化测试修改网格信息查询到了自动化测试修改网格信息")
        # self.page.searchTools.reset_btn.click()  # 点击重置按钮
        # sleep(1)


if __name__ == '__main__':
    unittest.main()
